package com.abcdefg.lotterysystem.common.result;

import com.abcdefg.lotterysystem.common.errorcode.ErrorCode;
import com.abcdefg.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;

@Data
public class CommonResult<T> implements Serializable {

    //返回错误码
    private Integer code;
    //返回正常数据
    private T data;
    //错误描述
    private String msg;

    //成功结果返回
    public static <T> CommonResult<T> success(T data) {
        CommonResult<T> result=new CommonResult<T>();
        result.setCode(GlobalErrorCodeConstants.SUCCESS.getCode());
        result.setData(data);
        result.setMsg("");
        return result;
    }
    //错误结果返回
    public static <T> CommonResult<T> error(Integer code,String message) {
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),
                "code 不是错误的异常");
        CommonResult<T> result=new CommonResult<T>();
        result.setCode(code);
        result.setMsg(message);
        return result;
    }
    //根据ErrorCode生成的返回统一结果集
    public static <T> CommonResult<T> error(ErrorCode errorCode) {
        return error(errorCode.getCode(), errorCode.getMsg());
    }


}
